package com.guinea.system.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.guinea.core.utils.StringUtils;
import com.guinea.system.entity.MenuRoleRel;
import com.guinea.system.form.MenuRoleRelForm;
import com.guinea.core.service.EntityService;
import com.guinea.core.web.StrutsEntityAction;
import com.guinea.system.service.MenuRoleRelService;
import com.guinea.system.entity.Role;
import com.guinea.system.service.RoleService;
import com.guinea.system.entity.Menu;
import com.guinea.system.service.MenuService;

public class MenuRoleRelAction extends StrutsEntityAction {
	private MenuRoleRelService menuRoleRelService;

	public EntityService getEntityService() {
		return menuRoleRelService;
	}

	public MenuRoleRelService getMenuRoleRelService() {
		return menuRoleRelService;
	}

	public void setMenuRoleRelService(MenuRoleRelService menuRoleRelService) {
		this.menuRoleRelService = menuRoleRelService;
	}
	
	private RoleService roleService;

	private Role role;

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public ActionForward cannotMissRole(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		role = null;

		String roleIdString = request.getParameter("roleId");
		if (StringUtils.isEmpty(roleIdString)) {
			saveError(request, "parameter.missing");
			return mapping.findForward(LIST);
		}
		Long roleId = new Long(roleIdString);

		role = getRoleService().getRole(roleId);
		if (role == null) {
			saveError(request, "entity.missing");
			return mapping.findForward(LIST);
		}
		return null;
	}
	
	private MenuService menuService;

	private Menu menu;

	public MenuService getMenuService() {
		return menuService;
	}

	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}

	public ActionForward cannotMissMenu(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		menu = null;

		String menuIdString = request.getParameter("menuId");
		if (StringUtils.isEmpty(menuIdString)) {
			saveError(request, "parameter.missing");
			return mapping.findForward(LIST);
		}
		Long menuId = new Long(menuIdString);

		menu = getMenuService().getMenu(menuId);
		if (menu == null) {
			saveError(request, "entity.missing");
			return mapping.findForward(LIST);
		}
		return null;
	}

	/**
	 * 显示Form表单时的回调函数.为Form对象添加更多属性,在子类重载.
	 */
	public void editRender(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		MenuRoleRelForm menuRoleRelForm = (MenuRoleRelForm) form;
		MenuRoleRel menuRoleRel = (MenuRoleRel) entity;
		
		if (entity != null) 
			menuRoleRelForm.setRoleId(menuRoleRel.getRole().getId());
		request.setAttribute("allRole", getRoleService().get());
		if (entity != null) 
			menuRoleRelForm.setMenuId(menuRoleRel.getMenu().getId());
		request.setAttribute("allMenu", getMenuService().get());
	}

	public ActionForward saveValidator(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ActionForward actionForward = super.saveValidator(mapping, form,
				request, response);
		
		if (actionForward == null)
			actionForward = cannotMissRole(mapping, form, request, response);
		if (actionForward == null)
			actionForward = cannotMissMenu(mapping, form, request, response);
		
		if(entity == null && getMenuRoleRelService().getByRoleMenu(role.getId(), menu.getId()).size() > 0){
			saveError(request, "entity.duplicated");
			return mapping.findForward(LIST);
		}

		return actionForward;
	}

	/**
	 * 保存业务对象的函数.
	 */
	protected void doSaveEntity(HttpServletRequest request) {
		MenuRoleRel menuRoleRel = (MenuRoleRel) entity;
		
		menuRoleRel.setRole(role);
		menuRoleRel.setMenu(menu);
		
		entity = menuRoleRel;
		super.doSaveEntity(request);
	}

}
